//
//  MainTabBar.swift
//  Weibo007
//
//  Created by rong on 15/6/24.
//  Copyright © 2015年 heima. All rights reserved.
//

import UIKit

class MainTabBar: UITabBar {

    //按钮总数
    private let buttonCount = 5;
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        let width = bounds.width / CGFloat(buttonCount)
        let rect = CGRectMake(0, 0, width, bounds.height)
        
        var index = 0;
        for v in subviews{
        
            if v is UIControl && !(v is UIButton)
            {
                v.frame = CGRectOffset(rect, CGFloat(index) * width, 0)
                
                index += index==1 ? 2 : 1
                
            }
            
            //设置“加号”按钮的布局
            compoesButton.frame = CGRectOffset(rect, 2*width, 0)
        }
    }
    
    //中间“加号”按钮的懒加载
    lazy var compoesButton: UIButton = {
    
        let btn = UIButton()
        
        btn.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        btn.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        
        self.addSubview(btn)
        
        return btn
    }()

}
